package com.wedo.user.controller;

import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.user.pojo.RequestUser;
import com.wedo.user.pojo.User;
import com.wedo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUser(@PathVariable("data")String data, @PathVariable("type")Integer type) {
        Boolean bool = this.userService.checkUser(data,type);
        if (bool == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(bool);
    }

    @PostMapping("code")
    public ResponseEntity<Void> sendVerifyCode(@RequestBody RequestUser requestUser) {
        this.userService.sendVerifyCode(requestUser.getPhone());
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("register")
    public ResponseEntity<Void> register(@RequestBody @Valid RequestUser user) {
        if (this.userService.register(user,user.getCode())) {
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("queryByUsername")
    public ResponseEntity<User> queryByUsernameAndPassword(@RequestParam("username")String username,@RequestParam("password")String password) {
        User result = this.userService.queryByUsernameAndPassword(username,password);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("queryByPhone")
    public ResponseEntity<User> queryByPhoneAndCode(@RequestParam("phone") String phone,@RequestParam("code") String code) {
        User result = this.userService.queryByPhoneAndCode(phone,code);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

}
